<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    //定时发布
    public function task()
    {
//        config('app_debug',1);
        if (isset($_GET['tpwd'])) {
            $tpwd = $_GET['tpwd'];
        } else {
            halt('error');
        }
        $list = Db::table('tbl_portal')->where('flag', 1)->select();
        foreach ($list as $portal) {
            $begin = time();
            $ip = $portal['ip'];
            $port = $portal['port'];
            $url = "http://$ip:$port/eportal/tmp/123fead50246387983ee340507115ef4.php?tpwd=$tpwd";
            trace($url);
//            exit;
            //设置超时参数
            $opts = array(
                "http" => array(
                    "method"  => "GET",
                    "timeout" => 3
                )
            );
            $context = stream_context_create($opts);
            echo "\r\n".$url;
            $result = @file_get_contents($url, false, $context);
            trace($result);
            echo '<br/>' . $ip . "\t" . $tpwd . "\t";
            $end = time();
//            if ($result == '[0]') {
                $upd = Db::table('tbl_portal')
                    ->where('ip', $ip)
                    ->where('port', $port)
                    ->update(['release_time' => date('Y-m-d H:i:s'), 'tpwd' => $tpwd]);
//            } else {
//                echo $result . "\t" . $end - $begin;
//            }

        }

    }

    public function details()
    {

        $orderNum = 0;
        $currDate = strtotime(date('Y-m-d'));
        $currMin = strtotime(date('Y-m-d H:i'));

        $endTime = $currDate - (($currMin - $currDate) * 60);
        $startTime = date('Y-m-d H:i:s', $endTime - 3600);
        $endTime = date('Y-m-d H:i:s', $endTime);
        echo ($startTime . ' to ' . $endTime) . '<br/>';

        //常规订单
        $result = order_details($startTime, $endTime, 1, '', 100);
        dump($result);
        if (isset($result['data']['results']['publisher_order_dto'])) {
            $orders = $result['data']['results']['publisher_order_dto'];
            while ($result['data']['has_next'] == true) {
                $pageNo = $result['data']['page_no'] + 1;
                $positionIndex = $result['data']['position_index'];
                $result = order_details($startTime, $endTime, $pageNo, $positionIndex, 100);
                $orders1 = array_merge($orders, $result['data']['results']['publisher_order_dto']);
            }

            foreach ($orders as $order) {
                try {
//                    db('orderDetails')->insert($order, true);
                } catch (\Exception $e) {
                    file_put_contents('/tmp/order_details', $e->getMessage(), 8);
                }
            }

            $orderNum = count($orders);
            dump('订单数量:' . $orderNum);

        }


        //记录最后时间
//        db('orderTask')->insert(['start_time'=>$startTime,'end_time'=>$endTime,'order_num'=>$orderNum]);
        echo 'order num: ' . $orderNum . '<br>';
        echo str_repeat('-', 80) . '<br>';

    }

    public function order_details()
    {
        $host = 'https://openapi.dataoke.com/api/tb-service/get-order-details';
        $version = 'v1.0.0';
        $params['orderScene'] = 1;//场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1
        if (input('orderScene')) {
            $params['orderScene'] = input('orderScene');
        }
        $params['queryType'] = 2;
        $params['endTime'] = date('Y-m-d H:i:s', strtotime('-0 minute'));
        $params['startTime'] = date('Y-m-d H:i:s', strtotime('-20 minute'));
        $result = dtk_request($host, $version, $params);
        $result = json_decode($result, true);
        if (isset($result['data']['results']['publisher_order_dto'])) {
            $orders = $result['data']['results']['publisher_order_dto'];
            foreach ($orders as $order) {
                try {
                    db('orderDetails')->strict(false)->insert($order, true);
                } catch (\Exception $e) {
                    file_put_contents('/tmp/order_details', $e->getMessage(), 8);
                }
            }
            dump($orders);
            $orderNum = count($orders);
            dump('订单数量:' . $orderNum);

        }
    }

    public function tpwdReport(){
        $data = db('tpwdReport')
//            ->where('update_time',null)
            ->order('update_time')
            ->limit(10)
            ->select();
        foreach ($data as $row){
            $report = get_tpwd_report($row['pid'],$row['tpwd']);
            db('tpwdReport')->where('tpwd',$row['tpwd'])
                ->update(array_merge($report,['update_time'=>date('Y-m-d H:i:s')]));
        }
        echo count($data);
    }
}
